package com.xuelang.pipeline.domain.resource.service.impl;

import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.resource.dto.SpaceQuery;
import com.xuelang.pipeline.domain.resource.Space;
import com.xuelang.pipeline.domain.resource.repository.SpaceRepository;
import com.xuelang.pipeline.domain.resource.service.SpaceDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 空间领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class SpaceDomainServiceImpl implements SpaceDomainService {

    @Autowired
    private SpaceRepository spaceRepository;

    @Override
    public PageRecord<Space> pageList(long currentPage, long pageSize, SpaceQuery spaceQuery) {
        return spaceRepository.pageList(currentPage, pageSize, spaceQuery);
    }

    @Override
    public List<Space> list(SpaceQuery spaceQuery) {
        return spaceRepository.list(spaceQuery);
    }

    @Override
    public Boolean save(Space space) {
        if(existsCode(space.getClusterUuid(), space.getCode())) {
            throw new BizException("空间编码已经存在");
        }
        if(existsName(space.getClusterUuid(), space.getCode(), space.getName())) {
            throw new BizException("空间名称已经存在");
        }
        return spaceRepository.save(space);
    }

    @Override
    public Boolean update(Space space) {
        return spaceRepository.update(space);
    }

    @Override
    public Boolean delete(String uuid) {
        return spaceRepository.delete(uuid);
    }

    @Override
    public Space query(String uuid) {
        return spaceRepository.query(uuid);
    }

    @Override
    public Boolean existsSpaceByClusterUuid(String clusterUuid) {
        return spaceRepository.existsSpaceByClusterUuid(clusterUuid);
    }

    @Override
    public Boolean existsCode(String targetClusterUuid, String spaceCode) {
        return spaceRepository.existsCode(targetClusterUuid, spaceCode);
    }

    @Override
    public Boolean existsName(String targetClusterUuid, String spaceCode, String spaceName) {
        return spaceRepository.existsName(targetClusterUuid, spaceCode, spaceName);
    }

    @Override
    public Space queryDefaultSpace() {
        return spaceRepository.queryDefaultSpace();
    }
}
